#include "MarsPrecompile.h"

#include "MarsVertexShader.h"
#include "MarsRenderer.h"
#include "MarsVRamBuffer.h"

using namespace Graphics;

VertexShader::VertexShader( const char* pcszFileName, const char* pcszEntry, const char* pcszShaderModel )
	: Shader( ShaderType_Vertex, pcszFileName, pcszEntry, pcszShaderModel )
	, m_pVertexShader( 0 )
	, m_bCompiled( false )
{
}

VertexShader::~VertexShader()
{
	Release();
}

void VertexShader::Release()
{
	m_bCompiled = false;

	SAFE_RELEASE( m_pVertexShader );

	Shader::Release();
}

bool VertexShader::PostCompile()
{
	HRESULT d3dResult;
	d3dResult = Renderer::Instance().GetDevice()->CreateVertexShader( m_pBuffer->GetBufferPointer(),
																	  m_pBuffer->GetBufferSize(), 
																	  0, 
																	  &m_pVertexShader );

	if ( FAILED( d3dResult ) )
	{
		SAFE_RELEASE( m_pBuffer );
		return false;
	}

	m_bCompiled = true;

	return true;
}

void VertexShader::SetCBufferToContext()
{
	ID3D11DeviceContext* pContext = Renderer::Instance().GetDeviceContext();

	ShaderSlotMapItr it = m_mapShaderSlots.begin();
	ShaderSlotMapItr end = m_mapShaderSlots.end();

	for ( ; it != end; ++it )
	{
		ID3D11Buffer* pBuffer = it->second.VBuffer->GetNativeBuffer();
		pContext->VSSetConstantBuffers( it->second.Index, 1, &pBuffer );
	}
}
